
package com.sakura.flowers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.flowers.dto.FlowerDTO;
import com.sakura.flowers.dto.OrdersDTO;
import com.sakura.flowers.entity.*;
import com.sakura.flowers.service.*;
import com.sakura.flowers.mapper.OrdersMapper;
import com.sakura.flowers.util.MyPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Sakura
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-02-18 21:39:53
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{
    @Autowired
    private FlowerService flowerService;
    @Autowired
    private OrderFlowerService orderFlowerService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderCardService orderCardService;


    @Override
    public Object queryByPage(Integer pageNo, Integer pageSize, Long uid) {
        Page<Orders> page = this.page(new Page<>(pageNo, pageSize),
                new LambdaQueryWrapper<Orders>().eq(Orders::getShopId, uid));
        List<OrdersDTO> ordersDTOList = page.getRecords().stream().map(item -> {
            OrdersDTO ordersDTO = new OrdersDTO();
            BeanUtils.copyProperties(item, ordersDTO);
            //设置购买人信息
            User user = userService.getById(item.getUserId());
            ordersDTO.setBuyUserName(user.getName());
            ordersDTO.setPhone(user.getPhone());
            return ordersDTO;
        }).collect(Collectors.toList());

        return new MyPage(ordersDTOList, page.getTotal());
    }

    @Override
    public Object getFlowerList(Long id) {
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        LambdaQueryWrapper<OrderFlower> orderCardWrapper = new LambdaQueryWrapper<>();
        orderCardWrapper.eq(OrderFlower::getOrderId, id);
        List<OrderFlower> flowerList = orderFlowerService.list(orderCardWrapper);
        double totalPrice = 0;
        if (flowerList.size() == 0) {
            result.put("list", new ArrayList<>());
            result.put("totalPrice", totalPrice);
            return result;
        }

        List<FlowerDTO> dtoList = new ArrayList<>();
        for (OrderFlower orderFlower : flowerList) {
            Flower flower = flowerService.getById(orderFlower.getFlowerId());
            FlowerDTO dto = new FlowerDTO();
            BeanUtils.copyProperties(flower, dto);
            dto.setSum(orderFlower.getNum());
            if (orderFlower.getNum() != null&&orderFlower.getNum()>=0) {
                totalPrice += flower.getPrice()*orderFlower.getNum();
            }
            dtoList.add(dto);
        }
        result.put("list", dtoList);
        result.put("totalPrice", totalPrice);
        Orders orders = this.getById(id);
        result.put("image", orders.getImage());
        return result;
    }
}




